package com.wis.tools;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wis.compress.CompressionDouglas;
import org.apache.storm.command.list;
import org.apache.storm.utils.Utils;

import java.util.*;

/**
 * @Description: traffic_wis
 * @author: fan
 * @Date: Created in 2018/12/19 15:24
 * @Modified By:
 */
public class MPGUtiles {
    /**
     * @param jsonKaZhi
     * @param njd       能见度的具体值
     * @return
     * @description 气象产品能见度卡值
     */
    public String vBKaZhi(JSONObject jsonKaZhi, String njd) {
        String[] kaZhiArr = jsonKaZhi.getString("threshold").split(",", -1);
        String[] colors = jsonKaZhi.getString("color").split(",", -1);
        String[] split = njd.split(",", -1);
        StringBuffer stringBuffer = new StringBuffer();
        for (int j = 0; j < split.length; j++) {
            if (j == 0 && split.length > 5) continue;
            float vbValue = Float.parseFloat(split[j]);
            for (int i = 0; i < kaZhiArr.length; i++) {
                if (vbValue > Integer.parseInt(kaZhiArr[i])) {
                    stringBuffer.append(colors[i] + "_" + vbValue);
                    break;
                }
            }
            if (vbValue <= Float.parseFloat(kaZhiArr[kaZhiArr.length - 1])) {
                stringBuffer.append(colors[kaZhiArr.length] + "_" + vbValue);
            }
            stringBuffer.append(",");
        }
        return stringBuffer.substring(0, stringBuffer.length() - 1);
    }

    /**
     * @param jsonKaZhi
     * @param common
     * @return
     * @description 气象产品通用的指标卡值方法
     */
    public String commonKaZhi(JSONObject jsonKaZhi, String common) {
        String[] kaZhiArr = jsonKaZhi.getString("threshold").split(",", -1);
        String[] colors = jsonKaZhi.getString("color").split(",", -1);
        String[] split = common.split(",", -1);
        StringBuffer stringBuffer = new StringBuffer();
        float kaZhiArrNum = 0;
        for (int j = 0; j < split.length; j++) {
            if (j == 0 && split.length > 5) continue;
            float commonValue = Float.parseFloat(split[j]);
            wc:
            for (int i = 0; i < kaZhiArr.length; i++) {
                if (jsonKaZhi.getString("id").equals("4")) {
                    kaZhiArrNum = Float.parseFloat(kaZhiArr[i]) * 100;
                } else {
                    kaZhiArrNum = Float.parseFloat(kaZhiArr[i]);
                }
                if (commonValue < kaZhiArrNum) {// 这儿涉及到异常值的处理
                    stringBuffer.append(colors[i] + "_" + commonValue);
                    break wc;
                }
            }

            if (jsonKaZhi.getString("id").equals("4")) {
                kaZhiArrNum = Float.parseFloat(kaZhiArr[kaZhiArr.length - 1]) * 100;
            } else {
                kaZhiArrNum = Float.parseFloat(kaZhiArr[kaZhiArr.length - 1]);
            }
            if (commonValue >= kaZhiArrNum) {
                stringBuffer.append(colors[kaZhiArr.length] + "_" + commonValue);
            }
            stringBuffer.append(",");
        }
        return stringBuffer.substring(0, stringBuffer.length() - 1);
    }

    public String jieBingNewKaZhi(JSONObject jsonKaZhi, String common) {
        String[] kaZhiArr = jsonKaZhi.getString("threshold").split(",", -1);
        String[] colors = jsonKaZhi.getString("color").split(",", -1);
        String[] split = common.split(",", -1);
        StringBuffer stringBuffer = new StringBuffer();
        for (int j = 0; j < split.length; j++) {
            if (j == 0 && split.length > 5) continue;
            float commonValue = Float.parseFloat(split[j]);
            if (commonValue != Float.parseFloat(kaZhiArr[1])) {
                stringBuffer.append(colors[1] + "_" + commonValue);
            } else {
                stringBuffer.append(colors[0] + "_" + commonValue);
            }
            stringBuffer.append(",");
        }
        return stringBuffer.substring(0, stringBuffer.length() - 1);
    }


    public String wDKaZhi(JSONObject jsonKaZhi, String fengsu) {
        String[] fs = fengsu.split(",", -1);
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < fs.length; i++) {
            float wsValue = Float.parseFloat(fs[i]);
            if (wsValue == -999) {
                stringBuffer.append("null_null");
            } else if (wsValue < 8) {
                stringBuffer.append(jsonKaZhi.get("level1") + "_" + wsValue);
            } else if (wsValue < 13.9) {
                stringBuffer.append(jsonKaZhi.get("level2") + "_" + wsValue);
            } else if (wsValue < 17.2) {
                stringBuffer.append(jsonKaZhi.get("level3") + "_" + wsValue);
            } else if (wsValue < 20.8) {
                stringBuffer.append(jsonKaZhi.get("level4") + "_" + wsValue);
            } else if (wsValue >= 20.8) {
                stringBuffer.append(jsonKaZhi.get("level5") + "_" + wsValue);
            } else {
                stringBuffer.append("null_null");
            }
            stringBuffer.append(",");
        }
        return stringBuffer.substring(0, stringBuffer.length() - 1);
    }


    public String pPKaZhi(JSONObject jsonKaZhi, String jiangshuiliang, String jiangshuixiangtai) {
        String[] jsl = jiangshuiliang.split(",", -1);
        String[] jsxt = jiangshuixiangtai.split(",", -1);

        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < jsl.length; i++) {
            float ppValue = Float.parseFloat(jsl[i]);
            float phValue = Float.parseFloat(jsxt[i]);
            if (ppValue == -999 || phValue == -999) {
                stringBuffer.append("null_null");
            } else if (phValue == 301) {// 雨
                if (ppValue < 0.1) {
                    stringBuffer.append(jsonKaZhi.getJSONObject("6").get("level1") + "_" + ppValue);
                } else if (ppValue < 1) {
                    stringBuffer.append(jsonKaZhi.getJSONObject("6").get("level2") + "_" + ppValue);
                } else if (ppValue < 3) {
                    stringBuffer.append(jsonKaZhi.getJSONObject("6").get("level3") + "_" + ppValue);
                } else if (ppValue < 5) {
                    stringBuffer.append(jsonKaZhi.getJSONObject("6").get("level4") + "_" + ppValue);
                } else if (ppValue < 10) {
                    stringBuffer.append(jsonKaZhi.getJSONObject("6").get("level5") + "_" + ppValue);
                } else if (ppValue < 15) {
                    stringBuffer.append(jsonKaZhi.getJSONObject("6").get("level6") + "_" + ppValue);
                } else if (ppValue >= 15) {
                    stringBuffer.append(jsonKaZhi.getJSONObject("6").get("level7") + "_" + ppValue);
                } else {
                    stringBuffer.append("null_null");
                }
            } else if (phValue == 302) {// 雪
                if (ppValue < 0.1) {
                    stringBuffer.append(jsonKaZhi.getJSONObject("7").get("level1") + "_" + ppValue);
                } else if (ppValue < 1) {
                    stringBuffer.append(jsonKaZhi.getJSONObject("7").get("level2") + "_" + ppValue);
                } else if (ppValue < 3) {
                    stringBuffer.append(jsonKaZhi.getJSONObject("7").get("level3") + "_" + ppValue);
                } else if (ppValue < 5) {
                    stringBuffer.append(jsonKaZhi.getJSONObject("7").get("level4") + "_" + ppValue);
                } else if (ppValue < 10) {
                    stringBuffer.append(jsonKaZhi.getJSONObject("7").get("level5") + "_" + ppValue);
                } else if (ppValue < 15) {
                    stringBuffer.append(jsonKaZhi.getJSONObject("7").get("level6") + "_" + ppValue);
                } else if (ppValue >= 15) {
                    stringBuffer.append(jsonKaZhi.getJSONObject("7").get("level7") + "_" + ppValue);
                } else {
                    stringBuffer.append("null_null");
                }
            } else if (phValue == 300) {
                stringBuffer.append(jsonKaZhi.getJSONObject("6").get("level1") + "_" + ppValue);
            } else if (phValue == 6) {// 雨夹雪-->雨
                if (ppValue < 0.1) {
                    stringBuffer.append(jsonKaZhi.getJSONObject("6").get("level1") + "_" + ppValue);
                } else if (ppValue < 1) {
                    stringBuffer.append(jsonKaZhi.getJSONObject("6").get("level2") + "_" + ppValue);
                } else if (ppValue < 3) {
                    stringBuffer.append(jsonKaZhi.getJSONObject("6").get("level3") + "_" + ppValue);
                } else if (ppValue < 5) {
                    stringBuffer.append(jsonKaZhi.getJSONObject("6").get("level4") + "_" + ppValue);
                } else if (ppValue < 10) {
                    stringBuffer.append(jsonKaZhi.getJSONObject("6").get("level5") + "_" + ppValue);
                } else if (ppValue < 15) {
                    stringBuffer.append(jsonKaZhi.getJSONObject("6").get("level6") + "_" + ppValue);
                } else if (ppValue >= 15) {
                    stringBuffer.append(jsonKaZhi.getJSONObject("6").get("level7") + "_" + ppValue);
                } else {
                    stringBuffer.append("null_null");
                }
            } else {
                stringBuffer.append("null_null");
            }
            stringBuffer.append(",");
        }
        return stringBuffer.substring(0, stringBuffer.length() - 1);
    }

    public String aTKaZhi(JSONObject jsonKaZhi, String qiwen) {
        String[] split = qiwen.split(",", -1);
        StringBuffer stringBuffer = new StringBuffer();
        for (String tmp : split) {
            float atValue = Float.parseFloat(tmp);
            if (atValue == -999) {
                stringBuffer.append("null_null");
            } else if (atValue <= -4) {
                stringBuffer.append(jsonKaZhi.get("level1") + "_" + atValue);
            } else if (atValue <= 4) {
                stringBuffer.append(jsonKaZhi.get("level2") + "_" + atValue);
            } else if (atValue < 35) {
                stringBuffer.append(jsonKaZhi.get("level3") + "_" + atValue);
            } else if (atValue < 40) {
                stringBuffer.append(jsonKaZhi.get("level4") + "_" + atValue);
            } else if (atValue >= 40) {
                stringBuffer.append(jsonKaZhi.get("level5") + "_" + atValue);
            } else {
                stringBuffer.append("null_null");
            }
            stringBuffer.append(",");
        }
        return stringBuffer.substring(0, stringBuffer.length() - 1);
    }

    public String rHKaZhi(JSONObject jsonKaZhi, String xiangduishidu) {
        String[] split = xiangduishidu.split(",", -1);
        StringBuffer stringBuffer = new StringBuffer();
        for (String tmp : split) {
            float rhValue = Float.parseFloat(tmp);
            if (rhValue == -999) {
                stringBuffer.append("null_null");
            } else if (rhValue < 20) {
                stringBuffer.append(jsonKaZhi.get("level1") + "_" + rhValue);
            } else if (rhValue < 40) {
                stringBuffer.append(jsonKaZhi.get("level2") + "_" + rhValue);
            } else if (rhValue < 60) {
                stringBuffer.append(jsonKaZhi.get("level3") + "_" + rhValue);
            } else if (rhValue < 80) {
                stringBuffer.append(jsonKaZhi.get("level4") + "_" + rhValue);
            } else if (rhValue <= 100) {
                stringBuffer.append(jsonKaZhi.get("level5") + "_" + rhValue);
            } else {
                stringBuffer.append("null_null");
            }
            stringBuffer.append(",");
        }
        return stringBuffer.substring(0, stringBuffer.length() - 1);
    }


    public String rIKaZhi(JSONObject jsonKaZhi, String daolujiebing) {
        String[] split = daolujiebing.split(",", -1);
        StringBuffer stringBuffer = new StringBuffer();
        for (String tmp : split) {
            float riValue = Float.parseFloat(tmp);
            if (riValue == -999) {
                stringBuffer.append("null_null");
            } else if (riValue == 0) {
                stringBuffer.append("#60b8ff" + "_" + riValue);
            } else if (riValue == 1) {
                stringBuffer.append("#a6a6a6" + "_" + riValue);
            } else {
                stringBuffer.append("null_null");
            }
            stringBuffer.append(",");
        }
        return stringBuffer.substring(0, stringBuffer.length() - 1);
    }

    public StringBuffer genMPGXianDataToJson(String tmpKeyValue, List tupleQueue, JSONObject jsonLatLon) {
        // List list = new LinkedList();
        JSONArray list = new JSONArray();
        StringBuffer stringBuffer = new StringBuffer();

        Map map = new HashMap();
        Map jsonMini = new HashMap();
        Map jsonObject_prop = new HashMap();
        JSONArray jsonMedium = new JSONArray();
        JSONObject jsonMax = new JSONObject();
        String[] elementValues = new String[]{};
        String[] elementValuesBefore = new String[]{};
        String[] elementValuesLast = new String[]{};

        String tmp = new String();
        String ttt = new String();
        try {
            // value：颜色+相态值+桩点
            // 颜色_300_G1_710
            // 传过来的集合中没有要素值为0的数据了，所以逻辑要重新改变一下
            for (int i = 0; i < tupleQueue.size(); i++) {
                tmp = tupleQueue.get(i).toString();

                elementValues = tupleQueue.get(i).toString().split("_", -1);
                if (i > 0) {
                    elementValuesBefore = tupleQueue.get(i - 1).toString().split("_", -1);
                } else {
                    elementValuesBefore = null;
                }
                if (i < tupleQueue.size() - 1) {
                    elementValuesLast = tupleQueue.get(i + 1).toString().split("_", -1);
                    ttt = tupleQueue.get(i + 1).toString();
                } else {
                    elementValuesLast = null;
                }

                if (elementValuesBefore != null) {// 前一个桩点存在
                    if (elementValuesBefore[0].equals(elementValues[0])) {// 和前一个值相等
                        if (Integer.parseInt(elementValues[4]) - 1 == Integer.parseInt(elementValuesBefore[4])) {// 和前一个桩点连续
                            // 添加
                            list.add(jsonLatLon.get(elementValues[3] + "_" + elementValues[4]));
                        } else {// 和前一个桩点不连续
                            if (list.size() > 1) {
                                jsonMedium = jsonMediumCom(jsonObject_prop, jsonMini, jsonMedium, map, elementValuesBefore[0], elementValuesBefore[2], elementValuesBefore[3] + "_" + elementValuesBefore[4], list, tmpKeyValue);
                                // list = new LinkedList();// 将集合清空
                            }
                            list.clear();
                            if (elementValuesLast != null) {// 后一个桩点存在
                                if (elementValues[0].equals(elementValuesLast[0])) { // 和后一个桩点的值相等
                                    if ((Integer.parseInt(elementValues[4]) + 1) == Integer.parseInt(elementValuesLast[4])) {// 和后一个桩点连续
                                        // 添加
                                        list.add(jsonLatLon.get(elementValues[3] + "_" + elementValues[4]));
                                    } else { // 和后一个桩点不连续
                                        // 检查
                                        if (list.size() > 1) {
                                            jsonMedium = jsonMediumCom(jsonObject_prop, jsonMini, jsonMedium, map, elementValues[0], elementValues[2], elementValues[3] + "_" + elementValues[4], list, tmpKeyValue);
                                            // list = new LinkedList();// 将集合清空
                                        }
                                        list.clear();
                                    }
                                }/* else {// 和后一个桩点的值不相等
                                    // 检查
                                    if (list.size() > 1) {
                                        jsonMedium = jsonMediumCom(jsonObject_prop, jsonMini, jsonMedium, map, elementValuesBefore[0], elementValuesBefore[2], elementValuesBefore[3] + "_" + elementValuesBefore[4], list, tmpKeyValue);                                      // list = new LinkedList();// 将集合清空
                                    }
                                    list.clear();
                                }*/
                            }/* else {// 后一个桩点不存在
                                // 检查
                                if (list.size() > 1) {
                                    jsonMedium = jsonMediumCom(jsonObject_prop, jsonMini, jsonMedium, map, elementValuesBefore[0], elementValuesBefore[2], elementValuesBefore[3] + "_" + elementValuesBefore[4], list, tmpKeyValue);                                   // list = new LinkedList();// 将集合清空
                                }
                                list.clear();
                            }*/
                        }
                    } else {// 和前一个值不相等
                        if (list.size() > 1) {
                            jsonMedium = jsonMediumCom(jsonObject_prop, jsonMini, jsonMedium, map, elementValuesBefore[0], elementValuesBefore[2], elementValuesBefore[3] + "_" + elementValuesBefore[4], list, tmpKeyValue);
                            // list = new LinkedList();// 将集合清空
                        }
                        list.clear();
                        if (elementValuesLast != null) {// 后一个桩点存在
                            if (elementValues[0].equals(elementValuesLast[0])) { // 和后一个桩点的值相等
                                if (Integer.parseInt(elementValues[4]) + 1 == Integer.parseInt(elementValuesLast[4])) {// 和后一个桩点连续
                                    // 添加
                                    list.add(jsonLatLon.get(elementValues[3] + "_" + elementValues[4]));
                                } /*else { // 和后一个桩点不连续
                                    // 检查
                                    if (list.size() > 1) {
                                        jsonMedium = jsonMediumCom(jsonObject_prop, jsonMini, jsonMedium, map, elementValuesBefore[0], elementValuesBefore[2], elementValuesBefore[3] + "_" + elementValuesBefore[4], list, tmpKeyValue);
                                        // list = new LinkedList();// 将集合清空
                                    }
                                    list.clear();
                                }*/
                            } /*else {// 和后一个桩点的值不相等
                                // 检查
                                if (list.size() > 1) {
                                    jsonMedium = jsonMediumCom(jsonObject_prop, jsonMini, jsonMedium, map, elementValuesBefore[0], elementValuesBefore[2], elementValuesBefore[3] + "_" + elementValuesBefore[4], list, tmpKeyValue);
                                    // list = new LinkedList();// 将集合清空
                                }
                                list.clear();
                            }*/
                        } /*else {// 后一个桩点不存在
                            // 检查
                            if (list.size() > 1) {
                                jsonMedium = jsonMediumCom(jsonObject_prop, jsonMini, jsonMedium, map, elementValuesBefore[0], elementValuesBefore[2], elementValuesBefore[3] + "_" + elementValuesBefore[4], list, tmpKeyValue);
                                // list = new LinkedList();// 将集合清空
                            }
                            list.clear();
                        }*/
                    }
                } else if (elementValuesLast != null) {// 前一个桩点不存，后一个存在           就是集合判断第一个参数的时候用到
                    if (list.size() > 1) {
                        jsonMedium = jsonMediumCom(jsonObject_prop, jsonMini, jsonMedium, map, elementValues[0], elementValues[2], elementValues[3] + "_" + elementValues[4], list, tmpKeyValue);
                        // list = new LinkedList();// 将集合清空
                    }
                    list.clear();
                    if (elementValues[0].equals(elementValuesLast[0])) { // 和后一个桩点的值相等
                        if (Integer.parseInt(elementValues[4]) + 1 == Integer.parseInt(elementValuesLast[4])) {// 和后一个桩点连续
                            // 添加
                            list.add(jsonLatLon.get(elementValues[3] + "_" + elementValues[4]));
                        }/* else { // 和后一个桩点不连续
                            // 检查
                            if (list.size() > 1) {
                                jsonMedium = jsonMediumCom(jsonObject_prop, jsonMini, jsonMedium, map, elementValuesBefore[0], elementValuesBefore[2], elementValuesBefore[3] + "_" + elementValuesBefore[4], list, tmpKeyValue);
                                // list = new LinkedList();// 将集合清空
                            }
                            list.clear();
                        }*/
                    }/* else {// 和后一个桩点的值不相等
                        // 检查
                        if (list.size() > 1) {
                            jsonMedium = jsonMediumCom(jsonObject_prop, jsonMini, jsonMedium, map, elementValuesBefore[0], elementValuesBefore[2], elementValuesBefore[3] + "_" + elementValuesBefore[4], list, tmpKeyValue);
                            // list = new LinkedList();// 将集合清空
                        }
                        list.clear();
                    }*/
                } /*else {// 后一个桩点不存在
                    // 检查
                    if (list.size() > 1) {
                        jsonMedium = jsonMediumCom(jsonObject_prop, jsonMini, jsonMedium, map, elementValuesBefore[0], elementValuesBefore[2], elementValuesBefore[3] + "_" + elementValuesBefore[4], list, tmpKeyValue);
                        // list = new LinkedList();// 将集合清空
                    }
                    list.clear();
                }*/
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (list.size() > 1) {
                jsonMedium = jsonMediumCom(jsonObject_prop, jsonMini, jsonMedium, map, elementValues[0], elementValues[2], elementValues[3] + "_" + elementValues[4], list, tmpKeyValue);
                // list = new LinkedList();// 将集合清空
            }

            jsonMax.put("type", "FeatureCollection");
            jsonMax.put("features", jsonMedium);

            if (jsonMax.toString().length() > 42) {
                stringBuffer.append(jsonMax);
                return stringBuffer;
            } else {
                return null;
            }
        }
    }

    private JSONArray jsonMediumCom(Map jsonObject_prop, Map mapMini, JSONArray jsonMedium, Map map, String color, String phValue, String idName, JSONArray list, String tmpKeyValue) {
        jsonObject_prop.put("ID", idName);
        jsonObject_prop.put("Color", color);//  #0066ff     蓝色
        jsonObject_prop.put("pst", phValue);
        mapMini.put("properties", JSON.toJSON(jsonObject_prop));
        map.put("type", "LineString");
        map.put("coordinates", CompressionDouglas.compression(list) /*JSON.toJSON(list)*/);
        mapMini.put("type", "Feature");
        mapMini.put("geometry", JSON.toJSON(map));
        jsonMedium.add(JSON.toJSON(mapMini));
        return jsonMedium;
    }
}
